#include<iostream>
using namespace std;

//剑指offer题19：输入一个矩阵，按照从外向里以顺时针的顺序依次打印出每一个数字，eg. 1  2  3  4
//                                                                                 5  6  7  8
//                                                                                 9  10 11 12
//                                                                                 13 14 15 16
//                                                       则依次打印出1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
#include<vector>
vector<int> PrintMaterix(vector<vector<int> > matrix)
{
	vector<int> v;

	int up = 0, down = matrix.size();
	int left = 0, right = matrix[0].size();
	
	while (up < down && left < right)//顺时针打印
	{
		int i = up, j = left - 1;
		while (++j < right)
			v.push_back(matrix[i][j]);//上横
		--j; if (up + 1 == down){ ++up; continue; }
		
		while (++i < down)
			v.push_back(matrix[i][j]);//右竖
		--i; if (right - 1 == left){ --right; continue; }
		
		while (--j >= left)
			v.push_back(matrix[i][j]);//下横
		++j;
		
		while (--i > up)
			v.push_back(matrix[i][j]);//左竖
		++i;
		
		up++; right--; down--; left++;
	}
	return v;
}
int main()
{
	int k = 1;
	const size_t n = 4;
	vector<vector<int> > v(n);
	for (size_t i = 0; i < v.size(); ++i)
	{
		const size_t m = 4;
		vector<int> arr(m);
		for (size_t j = 0; j < arr.size(); ++j)
			arr[j] = k++;
		
		v[i] = arr;
	}
	
	vector<int> tmp = PrintMaterix(v);
	//for (auto&r : tmp)cout << r << " "; cout << endl;
	int i = 0;
	for(;i < tmp.size(); ++i){
		cout<< tmp[i]<<" ";
	}cout <<endl;

	return 0;
}
